package com.cloudweb.oa.module.desktop;

import cn.js.fan.db.ResultIterator;
import cn.js.fan.db.ResultRecord;
import cn.js.fan.db.SQLFilter;
import cn.js.fan.util.ErrMsgException;
import com.cloudweb.oa.api.IDesktopCard;
import com.cloudwebsoft.framework.db.JdbcTemplate;
import com.redmoon.oa.flow.FormDb;
import com.redmoon.oa.visual.ModuleSetupDb;
import com.redmoon.oa.visual.ModuleUtil;
import com.redmoon.oa.visual.SQLBuilder;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;

public class DesktopCardModule implements IDesktopCard {

    DesktopCard desktopCard;

    public DesktopCardModule(DesktopCard desktopCard) {
        this.desktopCard = desktopCard;
    }

    @Override
    public String getTitle() {
        return desktopCard.getTitle();
    }

    @Override
    public int getStartVal() {
        return desktopCard.getStartVal();
    }

    @Override
    public int getEndVal(HttpServletRequest request) {
        ModuleSetupDb msd = new ModuleSetupDb();
        msd = msd.getModuleSetupDb(desktopCard.getModuleCode());
        if (msd == null) {
            return -1;
        }
        FormDb fd = new FormDb();
        fd = fd.getFormDb(msd.getString("form_code"));
        try {
            request.setAttribute(ModuleUtil.MODULE_SETUP, msd);
            String sql = SQLBuilder.getModuleListSqlAndUrlStr(request, fd, "", "", "")[0];
            sql = SQLFilter.getCountSql(sql);
            JdbcTemplate jt = new JdbcTemplate();
            ResultIterator ri = jt.executeQuery(SQLFilter.getCountSql(sql));
            if (ri.hasNext()) {
                ResultRecord rr = ri.next();
                return rr.getInt(1);
            }
        } catch (ErrMsgException | SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public boolean isLink() {
        return desktopCard.isLink();
    }

    @Override
    public String getUrl() {
        return "visual/module_list.jsp?code=" + desktopCard.getModuleCode();
    }

    @Override
    public String getUnit() {
        return desktopCard.getUnit();
    }

    @Override
    public String getBgColor() {
        return desktopCard.getBgColor();
    }

    @Override
    public String getIcon() {
        return desktopCard.getIcon();
    }
}
